package com.example.busshop.entity;

import com.example.busshop.common.entity.LogicEntity;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;
@Entity
@Setter
@Getter
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})

@Where(clause = "is_deleted=0")
@Table(name="shop")
public class Shop extends LogicEntity {
    @Column
    String name;

    @OneToMany(mappedBy = "shop",fetch = FetchType.LAZY,
    orphanRemoval = true,
    cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<ShopItem> shopItems;
    @OneToMany(mappedBy = "shop",
    fetch = FetchType.LAZY,
    cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Order> orders;

    @ManyToOne(fetch = FetchType.LAZY)
    private Business business;

    public Item addItem(Item item){
        if(isInShop(item))
            return item;
        ShopItem shopItem=new ShopItem();
        shopItem.setItem(item);
        shopItem.setStatus(1);
        return item;
    }

    private Boolean isInShop(Item item){
        if(shopItems.contains(item))
            return true;
        return false;

    }
    public ShopItem newItem(Item item){
        ShopItem shopItem=new ShopItem();
        shopItem.setStatus(0);
        shopItem.setName(item.getName());
        shopItem.setPrice(item.getPrice());
        shopItem.setCreateTime(new Date());
        shopItem.setItem(item);
        shopItems.add(shopItem);
        return shopItem;
    }



}
